package com.woniuxy.carlife.carlifeuserweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.carlife.carlifecommonsweb.exception.CarlifeGlobalException;
import com.woniuxy.carlife.carlifeuserweb.entity.Carinsurance;
import com.woniuxy.carlife.carlifeuserweb.mapper.CarinsuranceMapper;
import com.woniuxy.carlife.carlifeuserweb.service.CarinsuranceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zjq
 * @ClassName:CarinsuranceServiceImpl
 * @Description:车险信息
 * @date 2021/4/6 20:02
 **/

@Service
public class CarinsuranceServiceImpl extends ServiceImpl<CarinsuranceMapper, Carinsurance> implements CarinsuranceService {

    @Resource
    CarinsuranceMapper carinsuranceMapper;

    /*
     * 功能描述〈车险信息查看根据用户id token解析用户id〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/11 23:13
     * @throws
     */

    @Override
    public Carinsurance findCarinsuranceByUserId(Integer userId) {
        QueryWrapper<Carinsurance> carinsuranceQueryWrapper = new QueryWrapper<>();
        carinsuranceQueryWrapper.eq("user_id", userId);
        Carinsurance carinsurance = carinsuranceMapper.selectOne(carinsuranceQueryWrapper);

        return carinsurance == null ? null : carinsurance;
    }

    /*
     * 功能描述〈车险信息新增（用户注册时新增）〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/11 23:08
     * @throws
     */

    @Override
    public Boolean addCarinsuranceByUserId(Integer userId) {
        Carinsurance carinsurance = new Carinsurance();
        carinsurance.setUserId(userId);
        int insert = carinsuranceMapper.insert(carinsurance);

        return insert > 0 ? true : false;
    }

    /*
     * 功能描述〈车险信息修改〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/11 23:08
     * @throws
     */

    @Override
    public Boolean modifyCarinsuranceByUserId(Carinsurance carinsurance) {
        UpdateWrapper<Carinsurance> carinsuranceUpdateWrapper = new UpdateWrapper<>();
        carinsuranceUpdateWrapper.eq("user_id", carinsurance.getUserId());

        int update = carinsuranceMapper.update(carinsurance, carinsuranceUpdateWrapper);

        return update > 0 ? true : false;
    }
}
